package com.ieng.ormosia.web.controller.system;

import com.ieng.ormosia.common.action.PageAction;
import com.ieng.ormosia.common.exception.PrimaryKeyException;
import com.ieng.ormosia.common.utils.ActionR;
import com.ieng.ormosia.common.vo.ResponseData;
import com.ieng.ormosia.connect.model.Role;
import com.ieng.ormosia.connect.model.User;
import com.ieng.ormosia.connect.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/user")
public class UserController {
	@Autowired
	private UserService userService;

	@Operation(summary = "用户列表")
	@RequestMapping(value = "/list")
	public ResponseData list(@RequestBody(required = false) PageAction<User> pageAction){
		return ActionR.success(userService.getUserAll(pageAction.getPage(), pageAction.getSize()));
	}

	@Operation(summary = "查询用户列表")
	@RequestMapping(value = "/query")
	public ResponseData query(@RequestBody(required = false) PageAction<User> pageAction){
		return ActionR.success(userService.getUserHashQ(pageAction.getPage(), pageAction.getSize(), pageAction.getRecord()));
	}

	@Operation(summary = "用户回收站")
	@RequestMapping(value = "/recycle")
	public ResponseData recycle(@RequestBody(required = false) PageAction<User> pageAction){
		return ActionR.success(userService.getUserDelAll(pageAction.getPage(), pageAction.getSize()));
	}

	@Operation(summary = "物理删除权限")
	@DeleteMapping(value = "/del")
	public ResponseData del(@RequestParam("id") Long id){
		userService.delUser(id);
		return ActionR.send("删除成功");
	}

	@Operation(summary = "根据ID查询用户信息")
	@GetMapping
	public ResponseData get(@RequestParam("id") Long id){
		return ActionR.success(userService.getUser(id));
	}

	@Operation(summary = "添加用户")
	@PostMapping
	public ResponseData add(@Validated @RequestBody User user){
		userService.addUser(user);
		return ActionR.send("添加成功");
	}

	@Operation(summary = "修改用户")
	@PostMapping
	public ResponseData edit(@RequestBody User user){
		if(user.getId() == null){
			throw new PrimaryKeyException("user id is null");
		}
		userService.editUser(user);
		return ActionR.send("修改成功");
	}

	@Operation(summary = "逻辑删除权限")
	@DeleteMapping
	public ResponseData remove(@RequestParam("id") Long id){
		userService.removeRole(id);
		return ActionR.send("删除成功");
	}

}
